import Rectangle from './Rectangle.js';

const birdDom = document.querySelector('.bird');
const birdDomStyle = getComputedStyle(birdDom);
const birdWidth = parseInt(birdDomStyle.width);
const birdHeight = parseInt(birdDomStyle.height);
const birdTop = parseInt(birdDomStyle.top);
const birdLeft = parseInt(birdDomStyle.left);

const gameDom = document.querySelector('.game');
const gameDomHeight = parseInt(getComputedStyle(gameDom).height);
const landHeight = parseInt(getComputedStyle(document.querySelector('.land')).height);
export default class Sky extends Rectangle {
  constructor() {
    super(birdLeft, birdTop, birdWidth, birdHeight, 0, 0, birdDom);
    this.g = 2000;
    this.wingStatus = 1;
    this.timer = null;
  }
  move(duration) {
    super.move(duration);
    // 重力加速度
    this.ySpeed += this.g * duration;
  }

  onMove() {
    if (this.top >= gameDomHeight - landHeight - birdHeight) {
      this.top = gameDomHeight - landHeight - birdHeight;
      this.ySpeed = 0;
    } else if (this.top <= 0) {
      this.top = 0;
    }
  }

  jump() {
    this.ySpeed = -500;
  }

  wingWaveStart() {
    if (this.timer) {
      return;
    }
    this.timer = setInterval(() => {
      this.wingStatus ++;
      if (this.wingStatus === 4) {
        this.wingStatus = 1;
      }
      this.dom.className = `bird swing${this.wingStatus}`;
    }, 150);
  }
  wingWavePause() {
    clearInterval(this.timer);
    this.timer = null;
  }
}
